/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.fields.group 
{
	import net.babeuf.form.event.FieldEvent;
	import net.babeuf.form.fields.AbstractField;
	import net.babeuf.form.fields.checkbox.CheckBoxField;
	import net.babeuf.form.fields.IField;
	
	/**
	 * ...
	 * @author damien pegoraro
	 */
	public class RadioGroup extends AbstractField implements IField 
	{
		private var _boxes : Array ; 
		private var _default : int ; 
		private var _current : int ; 
		
		
		public function RadioGroup( defaultID : int = -1, ...boxes ) 
		{	
			
			_boxes = new Array() ; 
			
			for ( var i : uint = 0; i < boxes.length ; ++i ) 
			{
				if ( boxes[i] is CheckBoxField ) _boxes.push( boxes[i] ) ; 
				CheckBoxField(boxes[i]).addEventListener(FieldEvent.FIELD_CHANGED, _fieldChanged) ; 
				CheckBoxField(boxes[i]).addEventListener(FieldEvent.FOCUS_IN, _groupFocusIn) ; 
				CheckBoxField(boxes[i]).addEventListener(FieldEvent.FOCUS_OUT, _groupFocusOut) ; 
			}
			
			_default = defaultID ;
			_current = _default ; 
			super() ; 			
		}
		
		private function _groupFocusIn(e:FieldEvent):void 
		{
			dispatchEvent( e.clone() ) ; 
		}
		
		private function _groupFocusOut(e:FieldEvent):void 
		{
			dispatchEvent( e.clone() ) ; 
		}
		
		
		override public function setTabIndex(index:int): int 
		{
			for ( var i : uint = 0; i < _boxes.length ; ++i ) 
			{
				index = _boxes[i].setTabIndex(index) ; 				
			}			
			
			return index ; 
		}
		
		
		
		
		override public function dispose():void 
		{
			super.dispose();
			
			for ( var i : uint = 0; i < _boxes.length ; ++i ) 
			{
				_boxes[i].dispose() ; 
				CheckBoxField(_boxes[i]).removeEventListener(FieldEvent.FIELD_CHANGED, _fieldChanged) ; 
				CheckBoxField(_boxes[i]).removeEventListener(FieldEvent.FOCUS_IN, _groupFocusIn) ; 
				CheckBoxField(_boxes[i]).removeEventListener(FieldEvent.FOCUS_OUT, _groupFocusOut) ; 
			}
			
			_boxes = null ; 
		}
		
		
		private function _fieldChanged(e:FieldEvent):void 
		{
			var c : CheckBoxField = CheckBoxField(e.field) ; 
			
			if ( c.selected )  _current = _boxes.indexOf(c) ; 
			else 
			{
				if ( optional )  _current =  -1 ;
				else
				{
					_current = _boxes.indexOf(c) ; 
					c.setValue(true) ; 				
				}
			}			
			_repaint() ; 		
			_notifyChanged() ; 
		}
		
		
		protected function _repaint() : void
		{
			var l : uint = _boxes.length ; 
			var field : CheckBoxField ; 
			
			for ( var i : uint = 0 ; i < l  ; ++i )
			{
				field = CheckBoxField(_boxes[i]) ; 
				field.setValue(_current  == i ) ; 				
			}
		}
		
		override public function empty() : void 
		{
			_current = _default ; 
			_repaint() ; 
		}
		
		
		override public function getValue() : *
		{
			var l : uint = _boxes.length ; 
			var field : CheckBoxField ; 
			
			for ( var i : uint = 0 ; i < l  ; ++i )
			{
				field = CheckBoxField(_boxes[i]) ; 
				if ( field.selected  ) return field.getValue() ; 
			}
			
			return null ; 
		}
				
		
		override public function setValue( value : * ) : void
		{
			_current = int(value) ; 
			_repaint() ; 
		}
		
		override public function validate() : Boolean 
		{		
			super.validate();
			var ok : Boolean = optional ? true :  _current >= 0 ;
			if ( !ok )_error = _emptyError ; 
			if ( _error != "" ) {_updateSensors(false) ; return false ; }
			else return true ; 
		}

		
	
		
	}

}